package com.custom.base.dialog

import android.content.DialogInterface
import android.graphics.Color
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.custom.base.R
import com.custom.base.util.SDTimerUtil
import com.custom.base.util.SDTimerUtil.SDTimerListener
import com.custom.base.util.SDViewUtil.hide
import com.custom.base.util.SDViewUtil.layoutParamsLinearLayoutMM
import com.custom.base.util.SDViewUtil.setBackgroundDrawable
import com.custom.base.util.SDViewUtil.show

/**
 * 作者：
 * 时间：2021/1/14
 * 描述：SDDialogCustom
 */
open class SDDialogCustom : SDDialogBase() {
    var mTvTitle: TextView
    var mTvTip: TextView
    var mLlContent: LinearLayout
    var mTvCancel: TextView
    var mTvConfirm: TextView
    private var mListener: SDDialogCustomListener? = null
    private val mTimer = SDTimerUtil()
    private var mTvPrompt: TextView
    var MisEmpty = false //点击确定后是否禁止dismiss
    fun setmListener(mListener: SDDialogCustomListener): SDDialogCustom {
        this.mListener = mListener
        return this
    }

    init {
        val mView = LayoutInflater.from(context).inflate(R.layout.dialog_custom, null)
        mTvTitle = mView.findViewById(R.id.dialog_custom_tv_title)
        mTvPrompt = mView.findViewById(R.id.dialog_custom_tv_prompt)
        mTvTip = mView.findViewById(R.id.dialog_custom_tv_tip)
        mLlContent = mView.findViewById(R.id.dialog_custom_ll_content_view)
        mTvCancel = mView.findViewById(R.id.dialog_custom_tv_cancel)
        mTvConfirm = mView.findViewById(R.id.dialog_custom_tv_confirm)
        setDialogView(mView)
        initViewStates()
    }

    private fun initViewStates() {
        mTvTitle.visibility = View.GONE
        mTvCancel.visibility = View.GONE
        mTvConfirm.visibility = View.GONE
        mTvCancel.setOnClickListener(this)
        mTvConfirm.setOnClickListener(this)
    }

    fun setPromptC(prompt: String?) {
        mTvPrompt.visibility = View.VISIBLE
        mTvPrompt.text = prompt
    }

    fun setCustomView(view: View): SDDialogCustom {
        setCustomView(view, null)
        return this
    }

    fun setCustomView(view: View, params: LinearLayout.LayoutParams?): SDDialogCustom {
        var params = params
        mLlContent.removeAllViews()
        if (params == null) {
            params = layoutParamsLinearLayoutMM
        }
        mLlContent.addView(view, params)
        return this
    }

    fun setCustomView(layoutId: Int): SDDialogCustom {
        mLlContent.removeAllViews()
        LayoutInflater.from(context).inflate(layoutId, mLlContent, true)
        return this
    }

    fun setContentVisibility(visibility: Int): SDDialogCustom {
        mLlContent.visibility=visibility
        return this
    }

    private fun changeBackground() {
        if (mTvCancel.visibility == View.VISIBLE && mTvConfirm.visibility == View.VISIBLE) {
            setBackgroundDrawable(mTvCancel, backgroundBottomLeft)
            setBackgroundDrawable(mTvConfirm, backgroundBottomRight)
        } else if (mTvCancel.visibility == View.VISIBLE) {
            setBackgroundDrawable(mTvCancel, backgroundBottomSingle)
        } else if (mTvConfirm.visibility == View.VISIBLE) {
            setBackgroundDrawable(mTvConfirm, backgroundBottomSingle)
        }
    }

    // tip
    @JvmOverloads
    fun showTip(tip: String?, time: Long = 2000) {
        if (!TextUtils.isEmpty(tip) && time > 0) {
            mTvTip.text = tip
            show(mTvTip)
            mTimer.startWork(time, object : SDTimerListener {
                override fun onWorkMain() {
                    mTvTip.text = ""
                    hide(mTvTip)
                }

                override fun onWork() {}
            })
        }
    }

    // ---------------------------color
    fun setTextColorTitle(color: Int): SDDialogCustom {
        mTvTitle.setTextColor(color)
        return this
    }

    fun setTextColorCancel(color: Int): SDDialogCustom {
        mTvCancel.setTextColor(color)
        return this
    }
    fun setTextBackgroundCancel(resId: Int): SDDialogCustom {
        mTvCancel.setBackgroundResource(resId)
        return this
    }

    fun setTextColorConfirm(color: Int): SDDialogCustom {
        mTvConfirm.setTextColor(color)
        return this
    }
    fun setTextBackgroundConfirm(resId: Int): SDDialogCustom {
        mTvConfirm.setBackgroundResource(resId)
        return this
    }


    // ---------------------------text
    fun setTextTitle(text: String): SDDialogCustom {
        if (TextUtils.isEmpty(text)) {
            mTvTitle.visibility = View.GONE
        } else {
            mTvTitle.visibility = View.VISIBLE
            mTvTitle.text = text
        }
        return this
    }

    fun setTextCancel(text: String): SDDialogCustom {
        if (TextUtils.isEmpty(text)) {
            mTvCancel.visibility = View.GONE
        } else {
            mTvCancel.visibility = View.VISIBLE
            mTvCancel.text = text
        }
        return this
    }

    fun setTextConfirm(text: String): SDDialogCustom {
        if (TextUtils.isEmpty(text)) {
            mTvConfirm.visibility = View.GONE
        } else {
            mTvConfirm.visibility = View.VISIBLE
            mTvConfirm.text = text
        }
        return this
    }

    override fun onClick(v: View) {
        if (v === mTvCancel) {
            clickCancel(v)
        } else if (v === mTvConfirm) {
            clickConfirm(v)
        }
    }

    private fun clickCancel(v: View) {
        mListener?.onClickCancel(v, this@SDDialogCustom)
        dismissClick()
    }

    private fun clickConfirm(v: View) {
        mListener?.onClickConfirm(v, this@SDDialogCustom)
        if (!MisEmpty) {
            dismissClick()
        }
    }

    override fun onDismiss(dialog: DialogInterface) {
        mListener?.onDismiss(this@SDDialogCustom)
    }

    override fun dismiss() {
        mTimer.stopWork()
        super.dismiss()
    }

    interface SDDialogCustomListener {
        fun onClickCancel(v: View, dialog: SDDialogCustom){}
        fun onClickConfirm(v: View, dialog: SDDialogCustom){}
        fun onDismiss(dialog: SDDialogCustom){}
    }
}